package com.jz.community.basecomm.utils;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.Date;

/**
 * 运算
 *
 * @author OutTime
 */
public class OperationTool {

    public static double retain(double b) {
        BigDecimal aa = new BigDecimal(Double.toString(b));
        return aa.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();

    }

    // 加
    public static double add(double a, double b) {
        // DecimalFormat df = new DecimalFormat("#.00");
        BigDecimal aa = new BigDecimal(Double.toString(a));
        BigDecimal bb = new BigDecimal(Double.toString(b));
        BigDecimal cc = aa.add(bb);
        return cc.setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue();
        // return Double.parseDouble(df.format(sishewuru(a + b)));
        // return sishewuru(a + b);
    }

    // 减
    public static double sub(double a, double b) {
        // DecimalFormat df = new DecimalFormat("#.00");
        BigDecimal aa = new BigDecimal(Double.toString(a));
        BigDecimal bb = new BigDecimal(Double.toString(b));
        BigDecimal cc = aa.subtract(bb);
        return cc.setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue();
    }

    // 乘
    public static double mul(double a, double b) {
        // DecimalFormat df = new DecimalFormat("#.00");
        BigDecimal aa = new BigDecimal(Double.toString(a));
        BigDecimal bb = new BigDecimal(Double.toString(b));
        return aa.multiply(bb).doubleValue();
        // return Double.parseDouble(df.format(sishewuru(a * b)));
        // return sishewuru(a * b);
    }

    public static BigDecimal toBigDecimal(int digit, double a) {
        BigDecimal aa = new BigDecimal(a + "");
        return aa.setScale(digit, BigDecimal.ROUND_HALF_UP);
    }

    // ÷
    public static double div(double a, double b) {
        // DecimalFormat df = new DecimalFormat("#.00");
        BigDecimal aa = new BigDecimal(Double.toString(a));
        BigDecimal bb = new BigDecimal(Double.toString(b));
        return aa.divide(bb).setScale(2, BigDecimal.ROUND_HALF_UP)
                .doubleValue();
    }

    public static int divInt(double a, double b) {
        // DecimalFormat df = new DecimalFormat("#.00");
        BigDecimal aa = new BigDecimal(Double.toString(a));
        BigDecimal bb = new BigDecimal(Double.toString(b));
        return aa.divide(bb).setScale(2, BigDecimal.ROUND_HALF_UP)
                .intValue();
    }

    // dou
    public static double getDistance(double dis) {
        DecimalFormat df = new DecimalFormat("#.0");

        return Double.parseDouble(df.format(dis / 1000));

    }

    public static String doubleToStr(double dis) {
        BigDecimal bd = new BigDecimal(Double.toString(dis));
        double a = bd.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();

        return a + "";
    }

    public static String doubleToStrOne(double dis) {
        BigDecimal bd = new BigDecimal(Double.toString(dis));
        double a = bd.setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue();

        return a + "";
    }

    public static String doubleToStrOn(double dis) {

        BigDecimal bd = new BigDecimal(Double.toString(dis));
        double a = bd.setScale(0, BigDecimal.ROUND_HALF_UP).doubleValue();

        return a + "";
    }

    public static String doubleToStrThree(double dis) {
        BigDecimal bd = new BigDecimal(Double.toString(dis));

        double a = bd.setScale(3, BigDecimal.ROUND_HALF_UP).doubleValue();

        return a + "";
    }

    public static double sishewuru(double dou) {
        BigDecimal bd = new BigDecimal(Double.toString(dou));
        double a = bd.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
        return a;
    }

    // 时间比较
    public static boolean twoDateDistance(Date startDate, Date endDate) {
        long timeLong = endDate.getTime() - startDate.getTime();
        if (timeLong < 0) {
            return false;
        } else {
            return true;
        }
    }

    /**
     * 得到一个字符串的长度,显示的长度,一个汉字或日韩文长度为2,英文字符长度为1
     *
     * @return int 得到的字符串长度
     */
    public static int length(String s) {
        if (s == null){
            return 0;
        }
        char[] c = s.toCharArray();
        int len = 0;
        for (int i = 0; i < c.length; i++) {
            len++;
            if (!isLetter(c[i])) {
                len++;
            }
        }
        return len;
    }

    public static boolean isLetter(char c) {
        int k = 0x80;
        return c / k == 0 ? true : false;
    }

    public static String subZeroAndDot(String s) {
        if (s.indexOf(".") > 0) {
            s = s.replaceAll("0+?$", "");// 去掉多余的0
            s = s.replaceAll("[.]$", "");// 如最后一位是.则去掉
        }
        return s;
    }

}
